package miblab.school.views;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import miblab.school.R;
import miblab.school.ScreenSchoolActivity;
import miblab.school.SystemFunctions;
import miblab.school.lists.FileList;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;




public class ViewResults extends Activity{

	private static  final String TAG = ViewResults.class.getSimpleName();
	private String FN;
	private double[] timings = new double[4];
	public Random mNumGen  = new Random();
	List<PieItem> PieData = new ArrayList<PieItem>(0);


	public boolean internet = false;

	static Properties testProps = null;


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.scr2b);

		FN=Getfilename();
		timings=SystemFunctions.ParseString(FN);
		Button back= (Button) findViewById(R.id.button1);

		back.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent intent = new Intent(ViewResults.this,ScreenSchoolActivity.class);
				startActivity(intent);

			}
		} );

		int OverlayId = R.drawable.cam_overlay_big;
		PieDraw(OverlayId,timings);


	}
	//**********************On Pause ******************************************************		
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}
	@Override
	public void onBackPressed() {
		//Log.d("CDA", "onBackPressed Called");
		Intent intent = new Intent(ViewResults.this,FileList.class);
		startActivity(intent);
	}
	//**********************Get filename************************************************
	public String Getfilename()
	{		

		String FN = null;

		//To Write to an SD

		Bundle extras = getIntent().getExtras();
		if(extras !=null) {
			FN = extras.getString("EXTRA_FILENAME");
		}

		return FN; 
	}

	//*********************Plot Chart***********************************************

	public void PieDraw(int OverlayId, double[] TS)
	{
		int MaxCount =0;
		//Add Data
		for (int i = 0; i < 4 ; i++) {
			double ItemCount = TS[i];
			PieItem Item       = new PieItem();
			Item.Count = ItemCount;
			switch(i)
			{
			case 0:
			{
				Item.Label = "Home";
				break;
			}
			case 1:
			{
				Item.Label = "School";
				break;
			}
			case 2:
			{
				Item.Label = "Hospital";
				break;
			}
			case 3:
			{
				Item.Label = "Others";
				break;
			}
			}
			Item.Color = 0xff000000 + 256*256*mNumGen.nextInt(256) + 256*mNumGen.nextInt(256) + mNumGen.nextInt(256);
			Log.i(TAG,Item.Label);
			PieData.add(Item);
			MaxCount += ItemCount;
		}
		//------------------------------------------------------------------------------------------
		// Size => Pie size
		//------------------------------------------------------------------------------------------
		int Size_X = 350;
		int Size_Y = 350;
		//------------------------------------------------------------------------------------------
		// BgColor  => The background Pie Color
		//------------------------------------------------------------------------------------------
		int BgColor = 0xffa1a1a1;
		//------------------------------------------------------------------------------------------
		// mBackgroundImage  => Temporary image will be drawn with the content of pie view
		//------------------------------------------------------------------------------------------
		Bitmap mBackgroundImage = Bitmap.createBitmap(Size_X, Size_Y, Bitmap.Config.RGB_565);
		//------------------------------------------------------------------------------------------
		// Generating Pie view
		//------------------------------------------------------------------------------------------
		View_PieChart PieChartView = new View_PieChart( this );
		PieChartView.setLayoutParams(new LayoutParams(Size_X, Size_Y));
		PieChartView.setGeometry(Size_X, Size_Y, 2, 2, 2, 2, OverlayId);
		PieChartView.setSkinParams(BgColor);
		PieChartView.setData(PieData,MaxCount);
		PieChartView.invalidate();
		//------------------------------------------------------------------------------------------
		// Draw Pie Vien on Bitmap canvas
		//------------------------------------------------------------------------------------------
		PieChartView.draw(new Canvas(mBackgroundImage));
		PieChartView = null;
		//------------------------------------------------------------------------------------------
		// Create a new ImageView to add to main layout
		//------------------------------------------------------------------------------------------
		ImageView mImageView = new ImageView(this);
		mImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		mImageView.setBackgroundColor(BgColor);
		mImageView.setImageBitmap( mBackgroundImage );
		//------------------------------------------------------------------------------------------
		// Finaly add Image View to target view !!!
		//------------------------------------------------------------------------------------------
		LinearLayout TargetPieView =  (LinearLayout) findViewById(R.id.pie_container);
		TargetPieView.addView(mImageView);
	}




}





